home *** CD-ROM | disk | FTP | other *** search
- Path: wkaufman.us.oracle.com!wkaufman
- From: wkaufman@wkaufman.us.oracle.com (William Kaufman)
- Newsgroups: comp.lang.c
- Subject: Re: Extracting chars from shorts.
- Date: 14 Jan 1996 18:13:15 GMT
- Organization: Oracle Corporation, Redwood Shores CA
- Message-ID: <4dbh3r$4o8@inet-nntp-gw-1.us.oracle.com>
- References: <4d13i0$k3s@lastactionhero.rs.itd.umich.edu>
- NNTP-Posting-Host: wkaufman.us.oracle.com
-
- In article <4d13i0$k3s@lastactionhero.rs.itd.umich.edu> dogcow@monet.ccs.itd.umich.edu (Tom Spindler) writes:
- ] I'm trying to extracts individual bytes from a double. The following code
- ] doesn't work:
- [... type punning ...]
- ] One compiler complains that it's an illegal cast, whereas gcc says that the
- ] return value is an int.
- ]
- ] I could do something like ((char *)&v[0]), but then it would crash if I
- ] tried to do something with lo(42).
- ]
- ] Any suggestions? (I already know what endian the code is supposed to run on.)
-
- If you know that, you can drop the structure and replace your macros
- with,
-
- #include <limit.h> /* for CHAR_BIT definition */
-
- #define lo(x) ((x) & ((1 << CHAR_BIT) - 1))
- #define hi(x) (lo((x) >> CHAR_BIT))
-
- The first will strip off the least-significant byte (regardless of
- endianness and byte size); the second shifts the data down by a byte and
- strips off the LSB. These should handle any expression you've got,
- including ones with prefix and postfix operators.
-
- -- Bill K.
-
- Bill Kaufman | " While not a master of intellect, the blatantly
- wkaufman@us.oracle.com | obvious things we take for granted never
- | escape his keen eye! " -- Bob Burden
-